﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;

namespace IMIS_ERP.BLL
{
    public class Assistant
    {
        #region 设置Gridview鼠标经过的颜色
        /// <summary>
        /// 设置Gridview鼠标经过的颜色
        /// </summary>
        /// <returns>返回颜色代码</returns>
        public static string getColor1 = "#C5E6FB";
        #endregion

        #region 检查用户的输入，防止SQL注入
        /// <summary>自定义事件——检查用户的输入
        /// 	<remark>2010-12-17 朱胜贤 </remark>
        /// </summary>
        /// /// <param name="str">字符串</param>
        /// <returns>返回是否验证通过</returns>
        public static bool CheckParams(string str)
        {
            if (str.IndexOf(';') >= 0 || str.IndexOf('\'') >= 0 || str.IndexOf(',') >= 0 || str.IndexOf('=') >= 0 || str.IndexOf('"') >= 0)
            {
                return false;
            }
            return true;
        }
        #endregion

        public static string a1 = ConfigurationManager.AppSettings["a1"];
        public static string a2 = ConfigurationManager.AppSettings["a2"];
        public static string a3 = ConfigurationManager.AppSettings["a3"];
        public static string a4 = ConfigurationManager.AppSettings["a4"];

        /// <summary>得到字符串的MD5码
        ///     <remarks>2010-12-10 河大信管团队-朱胜贤</remarks>
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>返回字符串的MD5码</returns>
        public static string GetMD5(string str)
        {
            byte[] b = System.Text.Encoding.Default.GetBytes(str);
            b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
            {
                ret += b[i].ToString("x").PadLeft(2, '0');
            }
            return ret;
        }

        /// <summary>转化用户类型数据
        ///     <remarks>2010-12-10 河大信管团队-水亚亚</remarks>
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static DataSet ConvetToTypeName(DataSet ds)
        {
            int count = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                switch (dr["huType"].ToString())
                {
                    case "0":
                        ds.Tables[0].Rows[count]["huType"] = "财务人员";
                        break;
                    case "1":
                        ds.Tables[0].Rows[count]["huType"] = "业务人员";
                        break;
                    case "2":
                        ds.Tables[0].Rows[count]["huType"] = "经理";
                        break;
                    case "3":
                        ds.Tables[0].Rows[count]["huType"] = "安装测量人员";
                        break;
                }
                count += 1;
            }
            return ds;
        }

        /// <summary>名称转换
        /// 
        /// </summary>
        /// <param name="ds">原始的集合</param>
        /// <param name="refName">依据字段</param>
        /// <param name="refValue">转化的key/value组合</param>
        /// <returns>转化后数据集合</returns>
        public static DataSet ConvertToTypeName_Common(DataSet ds, string refName, Dictionary<object, string> refValue)
        {
            int count = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                foreach (string x in refValue.Keys)
                {
                    if (dr[refName].ToString() == x.ToString())
                    {
                        ds.Tables[0].Rows[count][refName] = refValue[x.ToString()];
                    }

                }
                count += 1;
            }
            return ds;
        }

        /// <summary>转换明文密码为MD5码，如果密码为空设为123
        ///     <remarks>2010-12-10 河大信管团队-朱胜贤</remarks>
        /// </summary>
        /// <param name="pwd">明文密码</param>
        /// <returns>返回MD5加密后的密码</returns>
        public static string AddToMD5(string pwd)
        {
            //转换明文密码为MD5码，如果密码为空设为123
            return GetMD5(pwd == "" ? "123" : pwd);
        }

        /// <summary>转换明文密码为MD5码，如果密码为空返回空值
        ///    <remarks>2010-12-10 河大信管团队-朱胜贤</remarks>
        /// </summary>
        /// <param name="pwd">明文密码</param>
        /// <returns>返回MD5加密后的密码</returns>
        public static string UpdateToMD5(string pwd)
        {
            //转换明文密码为MD5码，如果密码为空设为123
            return (pwd != "" ? Assistant.GetMD5(pwd) : "");
        }

        /// <summary>转换字符串 a,b,c 为 'a','b','c'
        ///    <remarks>2011-7-22 河大信管团队-朱胜贤</remarks>
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>返回转换后的字符串</returns>
        public static string StrToQuotation(string str)
        {
            string[] strList = str.Split(',');
            string strQ = "";
            for (int i = 0; i < strList.Length; i++)
            {
                if (i > 0)
                {
                    strQ += ",";
                }
                strQ += "'" + strList[i] + "'";
            }
            return strQ;
        }

    }
}
